package cn.dzhang.rcp.hyperbola;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import cn.dzhang.rcp.hyperbola.actions.AddContactAction;
import cn.dzhang.rcp.hyperbola.actions.ChatAction;
import cn.dzhang.rcp.hyperbola.actions.FillSeparator;
import cn.dzhang.rcp.hyperbola.common.UIConstants;
import cn.dzhang.rcp.hyperbola.common.UIUtils;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
	private IWorkbenchAction quitAction;
	private IWorkbenchAction aboutAction;
	private AddContactAction addContactAction;
	private ChatAction chatAction;
	private IWorkbenchAction actionPref;
	private IWorkbenchAction closeAllAction;
	private IWorkbenchAction saveAllAction;
	private IContributionItem viewsShortlist;
	private IContributionItem perspectiveShortlist;

	// private ChatAction chatAction;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	@Override
	protected void makeActions(IWorkbenchWindow window) {
		quitAction = ActionFactory.QUIT.create(window);
		quitAction.setImageDescriptor(UIConstants.ImageDescriptors.EXIT_16);
		register(quitAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		register(aboutAction);

		addContactAction = new AddContactAction(window);
		register(addContactAction);

		actionPref = ActionFactory.PREFERENCES.create(window);
		register(actionPref);

		chatAction = new ChatAction(window);
		register(chatAction);

		closeAllAction = ActionFactory.CLOSE_ALL.create(window);
		register(closeAllAction);

		saveAllAction = ActionFactory.SAVE_ALL.create(window);
		register(saveAllAction);

		perspectiveShortlist = ContributionItemFactory.PERSPECTIVES_SHORTLIST
				.create(window);
		viewsShortlist = ContributionItemFactory.VIEWS_SHORTLIST.create(window);

	}

	@Override
	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager hyperbolaMenu = new MenuManager("&Hyperbola", "hyperbola");
		hyperbolaMenu.add(addContactAction);
		hyperbolaMenu.add(chatAction);
		hyperbolaMenu.add(quitAction);

		MenuManager mnmgrWindow = new MenuManager("&Window");
		MenuManager mnmgrOpenPerspective = new MenuManager("Open Perspective",
				UIConstants.ImageDescriptors.COMMENTS_ADD_16,
				"open perspective");
		mnmgrOpenPerspective.add(perspectiveShortlist);
		mnmgrWindow.add(mnmgrOpenPerspective);

		MenuManager mnmgrShowView = new MenuManager("Show View",
				UIConstants.ImageDescriptors.COMMENTS_ADD_16, "show view");
		mnmgrShowView.add(viewsShortlist);

		mnmgrWindow.add(mnmgrShowView);
		mnmgrWindow.add(actionPref);

		MenuManager helpMenu = new MenuManager("&Help", "help");
		helpMenu.add(aboutAction);

		menuBar.add(hyperbolaMenu);
		menuBar.add(mnmgrWindow);
		menuBar.add(helpMenu);

	}

	public void fillTrayItem(IMenuManager menuManager) {
		menuManager.add(aboutAction);
		menuManager.add(quitAction);
	}

	@Override
	protected void fillStatusLine(IStatusLineManager statusLine) {
		super.fillStatusLine(statusLine);
	}

	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		ToolBarManager toolBar = new ToolBarManager(coolBar.getStyle()) {
			@Override
			public ToolBar createControl(Composite parent) {
				UIUtils.setDefaultBackgroundColor(parent);
				return super.createControl(parent);
			}
		};
		toolBar.add(quitAction);
		toolBar.add(addContactAction);
		toolBar.add(chatAction);

		toolBar.add(new FillSeparator(120));
		toolBar.add(saveAllAction);

		coolBar.add(toolBar);

	}
}
